﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;

    [ScriptType("SP.NavigationNode", ServerTypeId="{cd5d6053-7ffd-41ac-bf36-7b856320a122}")]
    public class NavigationNode : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public NavigationNode(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public void DeleteObject()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "DeleteObject", null);
            base.Context.AddQuery(query);
            base.RemoveFromParentCollection();
        }

        internal void InitFromCreationInformation(NavigationNodeCreationInformation creation)
        {
            if (creation != null)
            {
                base.ObjectData.Properties["Title"] = creation.Title;
                base.ObjectData.Properties["Url"] = creation.Url;
            }
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                string str = peekedName;
                if (str == null)
                {
                    return flag;
                }
                if (!(str == "Title"))
                {
                    if (str == "Id")
                    {
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Id"] = reader.ReadInt32();
                        return flag;
                    }
                    if (str == "Url")
                    {
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Url"] = reader.ReadString();
                        return flag;
                    }
                    if (str == "Children")
                    {
                        flag = true;
                        reader.ReadName();
                        this.Children.FromJson(reader);
                    }
                    return flag;
                }
                flag = true;
                reader.ReadName();
                base.ObjectData.Properties["Title"] = reader.ReadString();
            }
            return flag;
        }

        [Remote]
        public void Update()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "Update", null);
            base.Context.AddQuery(query);
            this.RefreshLoad();
        }

        [Remote]
        public NavigationNodeCollection Children
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("Children", out obj2))
                {
                    return (NavigationNodeCollection) obj2;
                }
                NavigationNodeCollection nodes = new NavigationNodeCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "Children"));
                base.ObjectData.ClientObjectProperties["Children"] = nodes;
                return nodes;
            }
        }

        [Remote]
        public int Id
        {
            get
            {
                base.CheckUninitializedProperty("Id");
                return (int) base.ObjectData.Properties["Id"];
            }
        }

        [Remote]
        public string Title
        {
            get
            {
                base.CheckUninitializedProperty("Title");
                return (string) base.ObjectData.Properties["Title"];
            }
            set
            {
                if (base.Context.ValidateOnClient)
                {
                    if (value == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("value");
                    }
                    if ((value != null) && (value.Length > 0x100))
                    {
                        throw ClientUtility.CreateArgumentException("value");
                    }
                }
                base.ObjectData.Properties["Title"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Title", value));
                }
            }
        }

        [Remote]
        public string Url
        {
            get
            {
                base.CheckUninitializedProperty("Url");
                return (string) base.ObjectData.Properties["Url"];
            }
            set
            {
                Uri uri;
                if ((base.Context.ValidateOnClient && (value != null)) && !Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out uri))
                {
                    throw ClientUtility.CreateArgumentException("value");
                }
                base.ObjectData.Properties["Url"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Url", value));
                }
            }
        }
    }
}

